Conditions | 1 |
Paths | 144 |
Total Lines | 76 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var Stubs = require('./Stub') |
||
13 | function Manager (context) { |
||
14 | var self = this |
||
15 | context = context || global |
||
16 | self._settings = {} |
||
17 | self._stash = null |
||
18 | |||
19 | function eachSymbol (callback) { |
||
20 | Object.keys(Stubs).forEach(function (symbol) { |
||
21 | var configuration = self._settings.global || {} |
||
22 | configuration = Objects.merge(configuration, self._settings[symbol] || {}) |
||
23 | callback(symbol, Stubs[symbol], configuration) |
||
24 | }) |
||
25 | } |
||
26 | |||
27 | function stash () { |
||
28 | if (self._stash) { |
||
29 | return |
||
30 | } |
||
31 | self._stash = {} |
||
32 | eachSymbol(function (symbol) { |
||
33 | if (context.hasOwnProperty(symbol)) { |
||
34 | self._stash[symbol] = context[symbol] |
||
35 | } |
||
36 | }) |
||
37 | } |
||
38 | |||
39 | function unstash () { |
||
40 | if (!self._stash) { |
||
41 | return |
||
42 | } |
||
43 | eachSymbol(function (symbol) { |
||
44 | if (self._stash.hasOwnProperty(symbol)) { |
||
45 | context[symbol] = self._stash[symbol] |
||
46 | } else { |
||
47 | delete context[symbol] |
||
48 | } |
||
49 | }) |
||
50 | delete self._stash |
||
51 | } |
||
52 | |||
53 | this.setup = function (settings) { |
||
54 | self._settings = settings |
||
55 | eachSymbol(function (symbol, _, configuration) { |
||
56 | if (!context[symbol] || !context[symbol]._setup) { |
||
57 | return |
||
58 | } |
||
59 | context[symbol]._setup(configuration) |
||
60 | }) |
||
61 | } |
||
62 | |||
63 | this.install = function () { |
||
64 | stash() |
||
65 | eachSymbol(function (symbol, Stub, configuration) { |
||
66 | var stub = typeof Stub === 'function' ? new Stub(configuration) : Stub |
||
67 | stub._setup && stub._setup(configuration) |
||
68 | context[symbol] = stub |
||
69 | }) |
||
70 | } |
||
71 | |||
72 | this.reset = function () { |
||
73 | eachSymbol(function (symbol) { |
||
74 | context[symbol] && context[symbol]._reset && context[symbol]._reset() |
||
75 | }) |
||
76 | } |
||
77 | |||
78 | this.flush = function () { |
||
79 | eachSymbol(function (symbol) { |
||
80 | context[symbol] && context[symbol]._flush && context[symbol]._flush() |
||
81 | }) |
||
82 | } |
||
83 | |||
84 | this.uninstall = function () { |
||
85 | self.flush() |
||
86 | unstash() |
||
87 | } |
||
88 | } |
||
89 | |||
93 |